package yxy.game.pm2.module.player;

import java.util.Collection;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.friendsdate;
import yxy.cherry.data.bean.guideinitial;
import yxy.cherry.data.bean.packagedata;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.bean.*;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.NotificationHandle;
import yxy.game.pm2.module.achievement.AchievementModule;
import yxy.game.pm2.module.arena.RankArena;
import yxy.game.pm2.module.avatar.HeadframeModule;
import yxy.game.pm2.module.avatar.TitleModule;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.conquest.RankConquest;
import yxy.game.pm2.module.daily.DailyModule;
import yxy.game.pm2.module.friend.FriendModule;
import yxy.game.pm2.module.friend.NeedFriendList;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;
import yxy.game.pm2.module.task.TaskHandle;

public class PlayerInitHandle extends PlayerHandle {
    public void initPlayer(PlayerBean player) {
        try {
            if (player.tryLock()) {
                if (player.getLevel() > 0) {
                    updatePlayer(player, true);
                    return;
                }
                guideinitial initialdata = DataCenter.getData(1, guideinitial.class);// 初始化数据集

                String playerCode = player.getCode();

                player.setLevel(initialdata.getPlayerlv());
                player.setUpgradeTime(DateTime.now());
                player.setName(player.getCode());
                // 初始模块开启

                // 初始化成就
                AchievementModule.init(playerCode);

                try {
                    // 初始武将
                    int heroId = initialdata.getInitialheroid();
                    Hero hero = new HeroHandle().createHero(playerCode, heroId, CCReason.首次登录);

                    // 初始武将入阵
                    FormatHandle formatModule = new FormatHandle();
                    formatModule.get(playerCode, FormatType.普通);

                    long power = new BattleHandle().power(playerCode, FormatType.普通);
                    player.setPower(power);
                    player.setHead(hero.getDataId());
                    new NotificationHandle().notifyBattlePower(playerCode, power);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                // 初始钱币和元宝
                if (initialdata.getInitialcoin() > 0) {
                    new CurrencyHandle().addCurrency(playerCode, CCReason.首次登录, Currency.create(CurrencyType.CT_钱币, 0, initialdata.getInitialcoin()));
                }
                if (initialdata.getInitialgold() > 0) {
                    new CurrencyHandle().addCurrency(playerCode, CCReason.首次登录, Currency.create(CurrencyType.CT_元宝, 0, initialdata.getInitialgold()));
                }

                // 初始形象
                new CurrencyHandle().addCurrency(playerCode, CCReason.首次登录, Currency.create(CurrencyType.CT_形象, initialdata.getAvatar(), 1));
                new CurrencyHandle().addCurrency(playerCode, CCReason.首次登录, Currency.create(CurrencyType.CT_头像, initialdata.getHeadid(), 1));
                new CurrencyHandle().addCurrency(playerCode, CCReason.首次登录, Currency.create(CurrencyType.CT_头像框, initialdata.getHeadframeid(), 1));
                player.setAvatar(initialdata.getAvatar());
                player.setHead(initialdata.getHeadid());
                player.setHeadframe(initialdata.getHeadframeid());

                updatePlayer(player, false);
                player.set();

                //新增初始化友情点，新手流程抽友情用
                friendsdate friendsdate = DataCenter.getData(1, friendsdate.class);// 初始化数据集
                new CurrencyHandle().addCurrency(playerCode, CCReason.首次登录, Currency.create(CurrencyType.CT_友情点, friendsdate.getReceivegetid()[0], 100));

                // 首登奖励邮件
                try {
                    packagedata _packagedata = DataCenter.getData(1, packagedata.class);
                    Collection<Currency> rewards = Currency.create(_packagedata.getRewardtype(), _packagedata.getRewardid(), _packagedata.getRewardnum());
                    String title = GameString.玩家登陆奖励邮件_title().getText();
                    String content = GameString.玩家登陆奖励邮件_msg().getText();
                    new MailHandle().sendMail(playerCode, MailTypeEnum.Mail, title, content, CCReason.首次登录, rewards);
                } catch (Exception e) {
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            player.unlock();
        }
    }

    /**
     * 更新玩家
     */
    private void updatePlayer(PlayerBean player, boolean set) {
        String playerCode = player.getCode();

        // 更新成就
        AchievementModule.init(playerCode);

        // 刷新日常任务
        DailyModule.get(playerCode);

        //背包初始化
        Item.init(playerCode);

        Sign.init(playerCode);

        // XXX 临时修复默认头像框被拿掉的问题
//		new CurrencyHandle().addCurrency(playerCode, CCReason.首次登录, Currency.create(CurrencyType.CT_头像框, 101, 1));

        // 统计型_登录_次数
        DateTime now = DateTime.now();
        DateTime time = player.getUpdateTime();
        if (time == null || !new LocalDate(now).equals(new LocalDate(time))) {
            player.setUpdateTime(now);
            TaskHandle.handle(playerCode, TaskType.统计型_登录_次数, 1);

            if (set) {
                player.set();
            }
        }

        // 好友
        {
            PlayerBean playerBean = PlayerBean.get(playerCode);
            ServerBean serverBean = ServerBean.get(playerBean.getServerId());
            AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

            FriendModule friendModule = FriendModule.get(playerCode);
            NeedFriendList needFriendList = new NeedFriendList(areaBean.getGroupId());
            needFriendList.update(friendModule);
        }

        HeadframeModule.all(playerCode);// 更新头像框

        TitleModule.all(playerCode);//更新称号

        // 排行榜头名检测推系统消息
        int rank = 0;
        try {
            // 战力榜
            rank = RankPlayerPower.getRank(playerCode).rank(playerCode);
            if (rank == 1) {
                new NotificationHandle().notifyChatInner(playerCode, GameString.战力排行榜头名上线推送(player.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            // 竞技场排行
            rank = RankArena.getRank(playerCode).rank(playerCode);
            if (rank == 1) {
                new NotificationHandle().notifyChatInner(playerCode, GameString.竞技场排行榜头名上线推送(player.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            // 征战榜
            rank = RankConquest.getRank(playerCode).rank(playerCode);
            if (rank == 1) {
                new NotificationHandle().notifyChatInner(playerCode, GameString.征战排行榜头名上线推送(player.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
